//
//  ViewController.m
//  UI_Homework_11
//
//  Created by 黄威 on 16/5/17.
//  Copyright © 2016年 黄威. All rights reserved.
//

#import "ViewController.h"
#import "SecondViewController.h"
@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic,retain)UITableView *table;
@property (nonatomic,retain)NSMutableDictionary *dic;
@property (nonatomic,retain)NSArray *arrOfKey;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self createTableView];
    [self createNavigationItem];
    [self handleData];
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    
    NSLog(@"%@",paths);
}

- (void)createNavigationItem{

self.navigationItem.title = @"通讯录🐰";
    
    self.navigationItem.rightBarButtonItem = self.editButtonItem;
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(add:)];
    

}

    
- (void)add:(UIBarButtonItem *)item{

    SecondViewController *second = [[SecondViewController alloc]init];
    [self.navigationController pushViewController:second animated:YES];
    

}

    
- (void)setEditing:(BOOL)editing animated:(BOOL)animated{


    [super setEditing:editing animated:YES];
    [_table setEditing:editing animated:YES];

}
    
- (void)handleData{
   NSString *path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"Contact.plist"];

    
    self.dic = [NSMutableDictionary dictionaryWithContentsOfFile:path];
    
    self.arrOfKey = [self.dic.allKeys sortedArrayUsingSelector:@selector(compare:)];
    
  
    
    
  
}


- (void)createTableView{

    self.table = [[[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain]autorelease];

    [self.view addSubview:_table];
    self.table.delegate = self;
    self.table.dataSource = self;
    
    [self.table registerClass:[UITableViewCell class] forCellReuseIdentifier:@"pool"];


    
}
    
    
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{


    return _arrOfKey.count;


}
    
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    NSString *key = [_arrOfKey objectAtIndex:section];
    
    NSArray *arr = [self.dic objectForKey:key];
    
    return arr.count;


}
    
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"pool"];
    
    NSString *key = [_arrOfKey objectAtIndex:indexPath.section];

    NSMutableArray *arr = [_dic objectForKey:key];
    
    NSMutableDictionary *tempdic = [arr objectAtIndex:indexPath.row];
    
    cell.textLabel.text = tempdic[@"name"];

    return cell;
}
    
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{


    return [_arrOfKey objectAtIndex:section];

}


- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{


    return YES;


}

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{

    if (indexPath.row == 0) {
        return NO;
    }else {
    
        return YES;
    }


}

-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{

    NSString *key = [_arrOfKey objectAtIndex:indexPath.section];
    NSMutableArray *arr = [_dic objectForKey:key];
    

    switch (editingStyle) {
        case UITableViewCellEditingStyleDelete:
            [arr removeObjectAtIndex: indexPath.row];
            
            [_table deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
            break;
            
            case UITableViewCellEditingStyleInsert:
            
        { NSDictionary *tempdic = @{@"name":@"boom",
                                      @"age":@"32",
                                      @"phonenumber":@"342423432"};
            [arr insertObject:tempdic atIndex:indexPath.row];
            
        }break;
        default:
            break;
    }

}


- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath{

    NSString *key = [_arrOfKey objectAtIndex:sourceIndexPath.section];
    
    NSMutableArray *arr = [_dic objectForKey:key];
    
    NSDictionary *tempdic = [[arr objectAtIndex:sourceIndexPath.row ]retain];
    
    [arr removeObjectAtIndex:sourceIndexPath.row];
    
    [arr insertObject:tempdic atIndex:destinationIndexPath.row];
    
    [tempdic release];
    
    
    
}

- (NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath
{
    if (sourceIndexPath.section == proposedDestinationIndexPath.section) {
        return proposedDestinationIndexPath;
    }else{
    
    
        return sourceIndexPath;
    }}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{


    NSString *key = [_arrOfKey objectAtIndex:indexPath.section];
    
    NSMutableArray *arr = [_dic objectForKey:key];
    
    NSMutableDictionary *tempdic = [arr objectAtIndex:indexPath.row];
    
    SecondViewController *second = [[SecondViewController alloc]init];
    second.Age = tempdic[@"age"];
    second.Name = tempdic[@"name"];
    second.Phonenumber = tempdic[@"phonenumber"];

    [self.navigationController pushViewController:second animated:YES];


}


- (void)viewWillAppear:(BOOL)animated{
    [self handleData];
    [_table reloadData];
}



    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    




























    
    
    
    
    
    
    
    
    
    
    
    








- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
